package cn.wolfcode.service.impl;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }
    @Override
    public void save(Employee employee, Long[] ids) {
        //对密码进行加密
        Md5Hash md5Hash = new Md5Hash(employee.getPassword(),employee.getName());
        employee.setPassword(md5Hash.toString());

        employeeMapper.insert(employee);
        //插入关系
        if (ids!=null && ids.length>0){
            for (Long roleId :ids){
                employeeMapper.insertRelation(employee.getId(),roleId);
            }
        }
    }
    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }
    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteRelation(employee.getId());
        //插入关系
        if (ids!=null && ids.length>0){
            for (Long roleId :ids){
                employeeMapper.insertRelation(employee.getId(),roleId);
            }
        }
    }
    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo(employees);
 
    }
    @Override
    public Employee login(String username, String password) {
        if (!StringUtils.hasLength(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        Employee employee = employeeMapper.login(username, password);
        if (employee==null){
            throw new RuntimeException("用户名密码不匹配");
        }
        return employee;
    }
    @Override
    public void updatePassword(Long id, String oldPassword, String newPassword) {
        //判空  旧密码
        if (StringUtils.isEmpty(oldPassword)){
            throw new RuntimeException("旧密码不能为空");
            //return new JsonResult(false,"密码不能为空");
        }
        // 新密码
        if (StringUtils.isEmpty(newPassword)) {
            throw new RuntimeException("新密码不能为空");
        }
        //密码重复
        if (oldPassword.equals(newPassword)){
            throw new RuntimeException("密码重复");
        }

        //获取主体
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        //对旧密吗进行解密
        Md5Hash hash = new Md5Hash(oldPassword,employee.getName());
        //校验密码   是否是本人操作
        if (employee.getPassword().equals(hash.toString())) {
            //对新密码进行加密
            Md5Hash md5Hash = new Md5Hash(newPassword,employee.getName());
            employee.setPassword(md5Hash.toString());
            employeeMapper.setPassword(employee.getId(), employee.getPassword());


        } else {
            throw new RuntimeException("旧密码错误");
        }
    }
    @Override
    public void updatePassword(Long id, String newPassword) {
        if (StringUtils.isEmpty(newPassword)){
            throw new RuntimeException("重置密码不能为空");
        }
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (employee.isAdmin()) {
            //对新密码进行加密
            Md5Hash md5Hash = new Md5Hash(newPassword,employee.getName());
            employee.setPassword(md5Hash.toString());
            employeeMapper.setPassword(id, employee.getPassword());
        }
    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.beatchDelete(ids);
        employeeMapper.deleteBeatchRelation(ids);
    }

    @Override
    public Employee selectByName(String name) {
        return employeeMapper.selectByName(name);
    }

    @Override
    public HSSFWorkbook exportXls() {
        //查询所有的员工
        List<Employee> employees = employeeMapper.selectAll();

        //创建一个excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet
        HSSFSheet sheet = workbook.createSheet("员工通讯录");
        //创建标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");

        for (int i = 0;i<employees.size();i++){
            //获取员工
            Employee employee = employees.get(i);
            //创建行
            row = sheet.createRow(i+1);
            //创建单元格
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            if (employee.getAge()!=null) {
                row.createCell(2).setCellValue(employee.getAge());
            }
        }
        return workbook;
    }

    @Override
    public void importXls(HSSFWorkbook workbook) {
        //获取第一页的数据
        HSSFSheet sheet = workbook.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <=lastRowNum ; i++) {
            //获取行数据
            HSSFRow row = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setEmail(row.getCell(1).getStringCellValue());

            HSSFCell cell = row.getCell(2);
            //根据单元格的类型来调用不同的方法获取
            if (cell.getCellType().equals(CellType.STRING)){
                String value = row.getCell(2).getStringCellValue();
                employee.setAge(Integer.valueOf(value));
            }else {
                //年龄
                double value = row.getCell(2).getNumericCellValue();
                employee.setAge((int)value);
            }
//            String value = row.getCell(2).getStringCellValue();
//            employee.setAge(Integer.valueOf(value));
            employee.setPassword("1");
            save(employee,null);
        }
    }

    @Override
    public List<Employee> selectByIds(HashSet<Long> ids) {
        ArrayList<Employee> list = new ArrayList<>();
        for (Long id:ids){
            Employee employee = employeeMapper.selectByPrimaryKey(id);
            list.add(employee);
        }
        return list;
    }

    @Override
    public void updateSelectById(Long id, boolean status) {
        employeeMapper.updateSeleteById(id,status);
    }

    @Override
    public Employee selectById(Long id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public List<Employee> selectByRoleSn(String... sns) {
        return employeeMapper.selectByRoleSn(sns);
    }

}
